/**
 * Created by pactera on 2018/5/17.
 */
var vm = new Vue({
    el:"#menu",
    data: {
        showList: false,
        showTreeList: false,
        showUrlList: false,
        selectRows:[],
        pageSize: 8,
        totalElements:0,
        formItem: {
            resourceType:'folder',
        },
        columns4: [
            {type: 'selection', width: 60,align: 'center'},
            {title: '名称',key: 'title'},
            {title: '资源类型',key: 'resourceType'},
            {title: 'url',key: 'url'},
            {title: '权限码',key: 'permission'},
            {title: '父级菜单',key: 'parentName'},
            {title: 'Icon',key: 'type'},
            {title: '描述',key: 'description'}
        ],
        data1: [],
        data2: [
            {
                title: '菜单',id:'-1',expand: false,loading: false,children: []
            }
        ]
    },
    mounted: function () {
        var _this = this;
        $.get("/menu/0?page=0&size="+this.pageSize, function(r){
            _this.data1 = r.data.content;
            _this.totalElements = r.data.totalElements;
        });

    },
    methods:{
        loadData: function (item, callback) {
            $.get("/menu/"+item.id, function(r){
                var treeData = r.data.content;
                callback(treeData);
            });
        },
        onSelect: function (selection,row) {
                this.selectRows = selection;
        },
        onSelectCancel: function (selection,row) {
            this.selectRows = selection;
        },
        handleSelectAll:function (status) {
            this.$refs.selection.selectAll(status);
        },
        updateMenu: function () {
            var rows = this.selectRows;
            if(rows && rows.length > 1){
                this.$Notice.warning({
                    title: '只能选择一条数据！'
                });
            }else if (rows && rows.length == 1){
                this.showList = true;
                this.formItem = this.selectRows[0];
            }else{
                this.$Notice.warning({
                    title: '请选择需要修改的数据！'
                });
            }
        },
        saveOrUpdate: function () {
            var _this = vm;
            $.ajax({
                url: '/menu/save',
                type: 'post',
                data: _this.formItem,
                success: function(result) {
                    if(result.code == 0){
                        _this.showList = false;
                        _this.getMenu();
                    }
                }
            });
        },
        deleteMenu: function () {
            var _this = vm;
            $.ajax({
                url: '/menu/delete',
                type: 'post',
                contentType: "application/json",
                data: JSON.stringify(_this.selectRows),
                success: function(result) {
                    if(result.code == 0){
                        _this.getMenu();
                        this.$Notice.warning({
                            title: '删除成功！'
                        });
                    }
                }
            });
        },
        menuTree: function () {
            this.showTreeList = false;
            this.formItem.parentId = this.$refs.tree.getSelectedNodes()[0].id;
            this.formItem.parentName = this.$refs.tree.getSelectedNodes()[0].title;
        },
        getMenu: function (page) {
            this.formItem = {};
            var _this = this;
            $.get("/menu/0?page="+page+"&size="+this.pageSize, function(r){
                _this.data1 = r.data.content;
                _this.totalElements = r.data.totalElements;
            });
        },
        changePage: function (index) {
            this.getMenu(index-1);
        }
    }
})
